package org.pay.manage.config.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.constant.PayConstant;
import org.pay.core.common.domain.PayResponse;
import org.pay.manage.common.service.RpcCommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: yangshancheng
 * @date: 18/1/17
 * @description:
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/data")
@PreAuthorize("hasRole('"+ MchConstant.MGR_ROLE_NORMAL+"')")
public class DataController {

    @Autowired
    private RpcCommonService rpcCommonService;

    /**
     * 统计数据
     * @return
     */
    @RequestMapping("/count")
    @ResponseBody
    public ResponseEntity<?> count(HttpServletRequest request) {

        int mchCount = rpcCommonService.rpcMchInfoService.count(null);
        int appCount = rpcCommonService.rpcMchAppService.count(null);
        List<Byte> statusList = new LinkedList<>();
        statusList.add(PayConstant.PAY_STATUS_SUCCESS);
        statusList.add(PayConstant.PAY_STATUS_COMPLETE);
        int payOrderCount = rpcCommonService.rpcPayOrderService.count(null, statusList);
        long totalAmount = rpcCommonService.rpcPayOrderService.sumAmount(null, statusList);

        JSONObject object = new JSONObject();
        object.put("mchCount", mchCount);
        object.put("appCount", appCount);
        object.put("payOrderCount", payOrderCount);
        object.put("totalAmount", totalAmount);

        return ResponseEntity.ok(PayResponse.buildSuccess(object));
    }

}
